import { computed, ComputedRef, reactive, ref, watchEffect } from 'vue'
import { defineStore } from 'pinia'

export const useUncertaintyStore = defineStore('Uncertainty', () => {
  // 测量参数读数次数
  // ASME PTC 19.1 1985: 第3.05节
  const desiredUCPercent = ref(1) // 预期的实验结果不确定度，单位为%
  const desiredUncertainty = computed(() => desiredUCPercent.value / 100) // 预期的试验结果不确定度
  const maximumAllowableInfluencePercent = computed(() => {// 分散性对结果的最大容许影响，单位%，图3.1
    return 0.1003 * Math.pow(desiredUCPercent.value, 0.5083)
  })
  const maximumAI = computed(() => {
    return maximumAllowableInfluencePercent.value * 0.01
  })
  const vibrant_para = ref<number>(1)
  const vibrant_option = [
    {
      label: '功率',
      value: 1
    }, {
      label: '用称重水箱测定流量（容积流量）',
      value: 2
    }, {
      label: '用流量喷嘴差压测定流量',
      value: 3
    }, {
      label: '蒸汽温度',
      value: 4
    }, {
      label: '蒸汽压力',
      value: 5
    }, {
      label: '给水温度',
      value: 6
    }, {
      label: '排汽压力',
      value: 7
    }
  ]

  // 读数1-5
  const readNum1 = ref(1)
  const readNum2 = ref(1)
  const readNum3 = ref(1)
  const readNum4 = ref(1)
  const readNum5 = ref(1)
  const unitStr = computed(() => {
    switch (vibrant_para.value) {
      case 1:
        return 'W'
      case 2:
        return 'm³/h'
      case 3:
        return 't/h'
      case 4:
        return '℃'
      case 5:
        return 'MPa'
      case 6:
        return '℃'
      case 7:
        return 'MPa'
      default:
        return 'W'
    }
  })

  // 将读数放入数组
  const readNumbers = [readNum1, readNum2, readNum3, readNum4, readNum5]

  // 计算最大值和最小值
  const maxReadNum = computed(() => Math.max(...readNumbers.map(num => num.value)))
  const minReadNum = computed(() => Math.min(...readNumbers.map(num => num.value)))
  const zBar = computed(() => { // 根据数据类型的不同，计算zBar的值
    let theta1 = 1
    if (vibrant_para.value === 1 || vibrant_para.value === 2) {
      theta1 = 1
    } else if (vibrant_para.value === 3) {
      theta1 = 0.5
    } else if (vibrant_para.value === 4) {

    }
  })

  return {
    desiredUncertainty,
    maximumAllowableInfluencePercent,
    maximumAI,
    desiredUCPercent,
    vibrant_para,
    vibrant_option,
    readNum1,
    readNum2,
    readNum3,
    readNum4,
    readNum5,
    unitStr,
    maxReadNum,
    minReadNum

  }
})

